package com.yuehuanghun.admin.generator.model;

import com.yuehuanghun.admin.generator.util.DatabaseDataTypesUtils;
import com.yuehuanghun.admin.generator.util.JavaPrimitiveTypeMapping;
import com.yuehuanghun.framework.util.StringUtils;

import lombok.Data;

@Data
public class Column {
	/**
	 * 所属表
	 */
	private Table table;

	/**
	 * JDBC类型
	 * 
	 * @see java.sql.Types
	 */
	private int jdbcType;

	/**
	 * JDBC类型名
	 */
	private String jdbcTypeName;

	/**
	 * 列名
	 */
	private String jdbcName;

	/**
	 * 是否主键
	 */
	private boolean isPk;

	/**
	 * 是否外键
	 */
	private boolean isFk;

	/**
	 * 长度
	 */
	private int size;

	/**
	 * 是否数字
	 */
	private int decimalDigits;

	/**
	 * 是否可空
	 */
	private boolean isNullable;

	/**
	 * 是否索引
	 */
	private boolean isIndexed;

	/**
	 * 是否唯一索引
	 */
	private boolean isUnique;

	/**
	 * 默认值
	 */
	private String defaultValue;

	/**
	 * 字段描述
	 */
	private String remarks;

	private String javaType;
	private String javaFieldName;
	private String columnComments;
	private boolean updatable = true;
	private boolean insertable = true;
	private boolean autoIncrement = false;

	public Column(Table table, int jdbcType, String jdbcTypeName, String jdbcName, int size, int decimalDigits,
			boolean isPk, boolean isNullable, boolean isIndexed, boolean isUnique, String defaultValue,
			String remarks) {
		this.table = table;
		this.jdbcType = jdbcType;
		this.jdbcName = jdbcName;
		this.jdbcTypeName = jdbcTypeName;
		this.size = size;
		this.decimalDigits = decimalDigits;
		this.isPk = isPk;
		this.isNullable = isNullable;
		this.isIndexed = isIndexed;
		this.isUnique = isUnique;
		this.defaultValue = defaultValue;
		this.remarks = remarks;
		this.javaFieldName = StringUtils.toCamelCase(jdbcName.matches("^[_A-Z]+$") ? jdbcName.toLowerCase() : jdbcName);
	}

	public Column() {
	}

	public int hashCode() {
		if (getTable() != null) {
			return (getTable().getJdbcName() + "#" + getJdbcName()).hashCode();
		} else {
			return (getJdbcName()).hashCode();
		}
	}

	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o instanceof Column) {
			Column other = (Column) o;
			if (getJdbcName().equals(other.getJdbcName())) {
				return true;
			}
		}
		return false;
	}

	public String toString() {
		return new StringBuilder("Column(").append("字段：").append(jdbcName).append(", 主键：").append(isPk).append(", 唯一键：")
				.append(isUnique).append(", 是否索引：").append(isUnique).append(isPk ? "，自增长：" + autoIncrement : "")
				.append(")").toString();
	}

	/**
	 * 使用 jdbcSqlType类型名称，示例值:VARCHAR,DECIMAL, 现Ibatis3使用该属性
	 */
	public String getJdbcSqlTypeName() {
		if (StringUtils.isNotBlank(jdbcTypeName)) {
			return jdbcTypeName;
		}
		return com.yuehuanghun.admin.generator.util.JdbcType.getJdbcSqlTypeName(getJdbcType());
	}

	/** 列是否是String类型 */
	public boolean getIsStringColumn() {
		return DatabaseDataTypesUtils.isString(getJavaType());
	}

	/** 列是否是日期类型 */
	public boolean getIsDateTimeColumn() {
		return DatabaseDataTypesUtils.isDate(getJavaType());
	}

	/** 列是否是Number类型 */
	public boolean getIsNumberColumn() {
		return DatabaseDataTypesUtils.isFloatNumber(getJavaType())
				|| DatabaseDataTypesUtils.isIntegerNumber(getJavaType());
	}

	/**
	 * 得到对应的javaType,如java.lang.String,
	 * 
	 * @return
	 */
	public String getJavaType() {
		return javaType;
	}

	/**
	 * 得到简短的java.lang.javaType,如java.lang.String将返回String,而非java.lang包的,将直接返回getJavaType()
	 * 
	 * @return
	 */
	public String getSimpleJavaType() {
		return StringUtils.replaceOnce(getJavaType(), "java.lang.", "");
	}

	/**
	 * 得到原生类型的javaType,如java.lang.Integer将返回int,而非原生类型,将直接返回getSimpleJavaType()
	 * 
	 * @return
	 */
	public String getPrimitiveJavaType() {
		return JavaPrimitiveTypeMapping.getPrimitiveType(getSimpleJavaType());
	}

	public String getColumnComments() {
		if (StringUtils.isNotBlank(columnComments)) {
			return columnComments;
		}
		if (StringUtils.isNotBlank(remarks)) {
			return remarks;
		}
		return javaFieldName;
	}
}
